<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>make qr code</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <style>
        input {
            height: 24px;
            width: 400px;
        }

        button {
            height: 24px;
            font-size: 16px;
        }
    </style>
</head>

<body>
    <h1>Weapp qrcode generator</h1>
    <!-- <p>This server is build for tester to create the test qrcode by themself</p>
    <p>In other ways,it is good for developer to save more time to do other thing, haha</p> -->
    <hr>
    <table>
        <tbody>
            <tr>
                <td>cli path</td>
                <td><input type="text" id="cliPath" value="" disabled></td>
                <td>微信开发者工具cli的全路径,windows要带上.bat</td>
            </tr>
            <tr>
                <td>post url</td>
                <td><input type="text" id="postUrl" value="" disabled></td>
                <td>请求url</td>
            </tr>
            <tr>
                <td>dist path</td>
                <td><input type="text" id="distPath" value="" disabled></td>
                <td>小程序编译后路径</td>
            </tr>
            <tr>
                <td>pathName</td>
                <td><input type="text" id="pathName" value=""></td>
                <td>要打开的页面,不填则显示首页, 例子: pages/transcript/transcript</td>
            </tr>
            <tr>
                <td>query</td>
                <td><input type="text" id="query" value=""></td>
                <td>要打开的页面的参数,不填默认为空, 例子: courseId=123&lessonId=456</td>
            </tr>
            <tr>
                <td></td>
                <td><button id="createBtn">create</button></td>
                <td></td>
            </tr>
        </tbody>
    </table>
    <hr>
    <h4>有效page值:</h4>
    <ul id="pageList"></ul>
    <hr>
    <div>
        <span id="status"></span>
        <div>
            <img id="qrcode" src="">
        </div>
    </div>

    <script>
        // default value
        const cliPath = '#default-cliPath#'
        const postUrl = '#default-postUrl#'
        const distPath = '#default-distPath#'
        const pageList = '#pageList#'

        let timer = null
        const startAnimate = () => {
            let i = 0
            timer = setInterval(() => {
                i = i > 4 ? 0 : i
                let str = '生成中' + '....'.substring(0, i)
                $('#status').html(str)
                i++
            }, 250)
        }
        const stopAnimate = () => {
            clearInterval(timer)
            $('#status').html('生成成功')
        }

        $(() => {
            // set default value
            $('#cliPath').val(cliPath)
            $('#postUrl').val(postUrl)
            $('#distPath').val(distPath)

            if (pageList) {
                try {
                    let list = JSON.parse(pageList)
                    let html = ''
                    for (let i of list) {
                        html += `<li>${i}</li>`
                    }
                    $('#pageList').html(html)
                } catch (e) {
                    console.error(e)
                }
            }

            // click and submit
            $('#createBtn').click(() => {
                let cliPath = $('#cliPath').val()
                let postUrl = $('#postUrl').val()
                let distPath = $('#distPath').val()
                let pathName = $('#pathName').val()
                let query = $('#query').val()
                startAnimate()
                $.post(postUrl, {
                    cliPath,
                    distPath,
                    pathName,
                    query
                }, (res) => {
                    stopAnimate()
                    $('#qrcode').attr('src', res)
                })
            })
        })
    </script>
</body>

</html>